"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 二级分区
Case Name   : list_hash二级分区表:表空间
Create At   : 2022/2/17
Owner       : @daiguatutu
Description :
    1.创建二级分区表，指定表空间,一级分区主键指定不同的表空间
    2.查看指定分区和索引的表空间
    3.创建二级分区表，指定表空间
    4.在不同的表空间为分区键创建唯一索引
    5.查看分区和分区索引的表空间
    6.清理环境
Expect      :
    1.成功
    2.表和索引的表空间不同
    3.成功
    4.成功
    5.表空间不同
    6.清理成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Subpartition(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.t_name = 't_subpartition_0143'
        self.ts_name_01 = 'ts_subpartition_0143_01'
        self.ts_name_02 = 'ts_subpartition_0143_02'

    def test_subpartition(self):
        text = '-----step1:创建二级分区表，指定表空间，' \
               '一级分区主键指定不同的表空间 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"drop tablespace if exists {self.ts_name_01}; " \
            f"create tablespace {self.ts_name_01} relative location " \
            f"'{self.ts_name_01}'; " \
            f"drop tablespace if exists {self.ts_name_02}; " \
            f"create tablespace {self.ts_name_02} relative location " \
            f"'{self.ts_name_02}'; " \
            f"create table if not exists {self.t_name} " \
            f"(col_1 int primary key using index tablespace " \
            f"{self.ts_name_02},  col_2 int ,col_3 int ,col_4 int " \
            f")tablespace {self.ts_name_01} " \
            f"partition by list (col_1) subpartition by hash (col_2) " \
            f"(partition p_list_1 values (-1,-2,-3,-4,-5,-6,-7,-8,-9,-10 ) " \
            f" (subpartition p_hash_1_1 , subpartition p_hash_1_2 , " \
            f"  subpartition p_hash_1_3 ), " \
            f" partition p_list_2 values (1,2,3,4,5,6,7,8,9,10 ) " \
            f" (subpartition p_hash_2_1 , subpartition p_hash_2_2 , " \
            f"  subpartition p_hash_2_3 , subpartition p_hash_2_4 , " \
            f"  subpartition p_hash_2_5 ), " \
            f" partition p_list_3 values (11,12,13,14,15,16,17,18,19,20), " \
            f" partition p_list_4 values (21,22,23,24,25,26,27,28,29,30 ) " \
            f" (subpartition p_hash_4_1 " \
            f" ), " \
            f" partition p_list_5 values (default) " \
            f" (subpartition p_hash_5_1 ), " \
            f" partition p_list_6 values (31,32,33,34,35,36,37,38,39,40) " \
            f" (subpartition p_hash_6_1 , subpartition p_hash_6_2 , " \
            f"  subpartition p_hash_6_3 )) enable row movement ; "
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step2:查看指定分区和索引的表空间 expect:成功,表和索引的表空间不同-----'
        self.log.info(text)
        sql = f"select t.spcname from pg_partition p, pg_tablespace t " \
            f"where p.reltablespace=t.oid and p.relname='{self.t_name}';" \
            f"select t.spcname from pg_partition p, pg_tablespace t " \
            f"where p.reltablespace=t.oid and p.relname='p_hash_6_1';" \
            f"select t.spcname from pg_partition p, pg_tablespace t " \
            f"where p.reltablespace=t.oid and p.relname='p_list_6';" \
            f"\d+ {self.t_name} "
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertEqual(4, msg.count(self.ts_name_01), '执行失败:' + text)
        self.assertIn(f'btree (col_1) TABLESPACE {self.ts_name_02}', msg,
                      '执行失败:' + text)

        text = '-----step3:创建二级分区表，指定表空间 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table if not exists {self.t_name}(col_1 int,col_2 int," \
            f"col_3 int ,col_4 int )tablespace {self.ts_name_01} " \
            f"partition by list (col_1) subpartition by hash (col_2) " \
            f"(partition p_list_1 values (-1,-2,-3,-4,-5,-6,-7,-8,-9,-10 ) " \
            f" (subpartition p_hash_1_1 , subpartition p_hash_1_2 , " \
            f"  subpartition p_hash_1_3 ), " \
            f" partition p_list_2 values (1,2,3,4,5,6,7,8,9,10 ) " \
            f" (subpartition p_hash_2_1 , subpartition p_hash_2_2 , " \
            f"  subpartition p_hash_2_3 , subpartition p_hash_2_4 , " \
            f"  subpartition p_hash_2_5 ), " \
            f" partition p_list_3 values (11,12,13,14,15,16,17,18,19,20), " \
            f" partition p_list_4 values (21,22,23,24,25,26,27,28,29,30 ) " \
            f" (subpartition p_hash_4_1 " \
            f" ), " \
            f" partition p_list_5 values (default) " \
            f" (subpartition p_hash_5_1 ), " \
            f" partition p_list_6 values (31,32,33,34,35,36,37,38,39,40) " \
            f" (subpartition p_hash_6_1 , subpartition p_hash_6_2 , " \
            f"  subpartition p_hash_6_3 )) enable row movement ; "
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step4:在不同的表空间为分区键创建唯一索引 expect:成功-----'
        self.log.info(text)
        sql = f"create unique index on {self.t_name}( col_1,col_2) " \
            f"tablespace {self.ts_name_02};"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, msg,
                      '执行失败:' + text)

        text = '-----step5:查看分区和分区索引的表空间 expect:表空间不同-----'
        self.log.info(text)
        sql = f"select t.spcname from pg_partition p, pg_tablespace t " \
            f"where p.reltablespace=t.oid and p.relname='p_hash_6_1';" \
            f"select t.spcname from pg_partition p, pg_tablespace t " \
            f"where p.reltablespace=t.oid and " \
            f"p.relname='p_hash_6_3_col_1_col_2_idx';"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        sub_ts = msg.splitlines()[-7].strip()
        sub_idx_ts = msg.splitlines()[-2].strip()
        self.assertNotEqual(sub_ts, sub_idx_ts, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----step6:清理环境-----')
        text_1 = '-----删除表 expect:成功-----'
        self.log.info(text_1)
        del_cmd = f'drop table if exists {self.t_name};' \
            f'drop tablespace if exists {self.ts_name_01};' \
            f'drop tablespace if exists {self.ts_name_02};'
        msg = self.pri_sh.execut_db_sql(del_cmd)
        self.log.info(msg)

        self.assertIn(self.constant.TABLE_DROP_SUCCESS, msg, '执行失败:' + text_1)
        self.assertEqual(2, msg.count(self.constant.TABLESPCE_DROP_SUCCESS),
                         '执行失败:' + text_1)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
